/*
* @(#)ListWorkflowRecordGroupAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.workflow.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowGroup;
import com.dcivision.workflow.bean.WorkflowRecord;
import com.dcivision.workflow.dao.WorkflowGroupDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;

/**
 * <p>Class Name:       ListWorkflowRecordGroupAction.java    </p>
 * <p>Description:      The list action class for ListWorkflowGroup.jsp</p>
 *
 * @author           Angus Shiu
 * @company          DCIVision Limited
 * @creation date    13/08/2003
 * @version          $Revision: 1.9 $
 */

public class ListWorkflowRecordGroupAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.9 $";

  public static final String VAR_WORKFLOW_RECORD_LIST = "listWorkflowRecordList";
  public static final String VAR_ACTIVE_RECORD_LIST = "listActiveRecord";

  /** Creates a new instance of ListWorkflowRecordGroupAction */
  public ListWorkflowRecordGroupAction() {
    super();
    this.setListName("workflowRecordGroupList");
    this.setListFunctionName("getAccessibleList");
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_RECORD);
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowGroupDAObject");
  }

  /**  Method getListData() - retrieve the appropriate item list for current page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws Exception if cannot get the list data from DAO object
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.getListData(mapping, form, request, response);

    WorkflowGroupDAObject dao = (WorkflowGroupDAObject) this.getMajorDAObject(request);
    WorkflowRecordDAObject recordDAO = (WorkflowRecordDAObject) this.getDAObjectByClassName(request, "com.dcivision.workflow.dao.WorkflowRecordDAObject");

    List lastWorkflowRecordList = new ArrayList(), noOfActiveRecordList = new ArrayList();
    List resultList = (List)request.getAttribute(this.m_sListName);

    for (int i=0; i<resultList.size(); i++) {
      WorkflowGroup workflowGroup = (WorkflowGroup) resultList.get(i);
      Integer activeRecordCount = recordDAO.getNoOfActiveRecordByWorkflowGroupID(workflowGroup.getID());
      noOfActiveRecordList.add(activeRecordCount);
      if (activeRecordCount.intValue()>0) {
        WorkflowRecord lastWorkflowRecord = (WorkflowRecord)recordDAO.getLastUpdateObjectByWorkflowGroupID(workflowGroup.getID());
        lastWorkflowRecordList.add(lastWorkflowRecord);
      } else {
        lastWorkflowRecordList.add(new WorkflowRecord());
      }
    }

    request.setAttribute(VAR_ACTIVE_RECORD_LIST, noOfActiveRecordList);
    request.setAttribute(VAR_WORKFLOW_RECORD_LIST, lastWorkflowRecordList);
  }

}
